import 'package:flutter/material.dart';

class AnimationStructure extends StatefulWidget {
  @override
  _AnimationStructureState createState() => _AnimationStructureState();
}

class _AnimationStructureState extends State<AnimationStructure>
    with SingleTickerProviderStateMixin {
  final _textStyle = TextStyle(
    fontSize: 20,
  );

  AnimationController _animationController;
  Animation<double> _animation;
  Animation _curve;
  double _value;
  AnimationStatus _status;

  @override
  void initState() {
    super.initState();
    _animationController = new AnimationController(
        duration: Duration(milliseconds: 1500), vsync: this);
    _curve =
        CurvedAnimation(parent: _animationController, curve: Curves.easeIn);
    _animation = Tween(begin: 0.0, end: 255.0).animate(_curve)
      ..addListener(() {
        setState(() {
          _value = _animation.value;
        });
      })
      ..addStatusListener((status) {
        setState(() {
          _status = status;
        });
      });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('动画的结构'),
      ),
      body: SingleChildScrollView(
        child: Column(
          children: <Widget>[
            Text(
              'Animation：抽象类拥有当前动画的值和状态',
              style: _textStyle,
            ),
            Text(
              'AnimationController：一定时间内生成变化的值（0.0~1.0），用于动画的控制',
              style: _textStyle,
            ),
            Text(
              'Tween：可以配置动画生成的值和范围',
              style: _textStyle,
            ),
            Text(
              'Curve：可以设置生成值的不同曲线',
              style: _textStyle,
            ),
            Text(
              'addListener：可以设置动画值变化的监听器',
              style: _textStyle,
            ),
            Text(
              'addStatusListener：可以设置动画状态的监听器',
              style: _textStyle,
            ),
            RaisedButton(
              colorBrightness: Brightness.dark,
              onPressed: () {
                if (_animation.status == AnimationStatus.completed) {
                  _animationController.reverse();
                } else {
                  _animationController.forward();
                }
              },
              child: _animation.status == AnimationStatus.completed
                  ? Text('反向动画')
                  : Text('正向动画'),
            ),
            Text('动画中变化的值：$_value'),
            Text('动画状态：$_status'),
            Container(
              height: _animation.value,
              width: _animation.value,
              child: FlutterLogo(),
            ),
          ],
        ),
      ),
    );
  }
}
